package com.sword.common.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @description: 字符串工具类
 * @author：Lv.RJ
 * @version: 1.0.0
 * @create: 2025/02/19
 **/
public class StrUtil {

    /**
     * 将Map中所有key的分隔符转换为新的分隔符
     * @param map map对象
     * @param separator 旧分隔符
     * @param newSeparator 新分隔符
     */
    public static void replaceKeysSeparator(Map<String, Object> map, String separator, String newSeparator) {
        if(map==null || map.isEmpty()) {
            return;
        }
        List<String> keySet = new ArrayList<>(map.keySet());
        for(String key : keySet) {
            if(StrUtil.isEmpty(key) || !key.contains(separator)) {
                continue;
            }
            String value = String.valueOf(map.get(key));
            String newKey = key.replaceAll(separator, newSeparator);
            map.putIfAbsent(newKey, value);
            map.remove(key);
        }
    }

    public static boolean isEmpty(String str) {
        return str == null || str.trim().length() == 0;
    }
    public static boolean isEmpty(CharSequence str) {
        return str == null || str.length() == 0;
    }
    public static boolean isBlank(CharSequence str) {
        int length;
        if (str != null && (length = str.length()) != 0) {
            for(int i = 0; i < length; ++i) {
                if (!CharUtil.isBlankChar(str.charAt(i))) {
                    return false;
                }
            }

            return true;
        } else {
            return true;
        }
    }

    public static boolean isNotEmpty(CharSequence str) {
        return !isEmpty(str);
    }
}
